const express = require('express')
const qs = require('querystring')
//路由容器
const shopstage = express.Router()
const axios = require('axios')
const multipart = require('connect-multiparty');
const FormData = require('form-data')
var multipartMiddleware = multipart()
const fs = require("fs");
const { JsonWebTokenError } = require('jsonwebtoken')
const jwt = require("jsonwebtoken");
const { CONNREFUSED } = require('dns');
const { json } = require('body-parser');
//路由容器
// 查询所有店铺
shopstage.get('/bussiness/getAllShops', (req, res) => {
    // console.log(req.query)
    axios({
        url: '/bussiness/getAllShops',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            token:req.session.token
        },
        method: 'get',
        params: {
            userId:jwt.decode(req.session.token).id
        }
    }).then(function(data){
        res.status(200).json({
            data:data.data,
            code:0
        })
    }).catch(function(err){
        res.status(500).json({
            msg:err
        })
    })
})
//注册店铺
// shopstage.post('/bussiness/addShop', (req, res) => {
//     let {cover,introduction,serviceNumber,shopAddress,shopName} = req.body.params;
//     axios({
//         url: '/bussiness/addShop',
//         headers: {
//             'Content-Type': 'application/x-www-form-urlencoded',
//             token:req.session.token
//         },
//         method: 'post',
//         params: {
//             cover:cover,
//             introduction:introduction,
//             serviceNumber:serviceNumber,
//             shopAddress:shopAddress,
//             shopName:shopName,
//             userId:jwt.decode(req.session.token).id,
//         }
//     }).then(function(data){
//         res.status(200).json({
//             data:data.data,
//             code:0
//         })
//     }).catch(function(err){
//         res.status(500).json({
//             msg:err
//         })
//     })
// })
shopstage.post('/bussiness/addShop',multipartMiddleware, (req, res) => {
    console.log(req.query)
        let {introduction,shopAddress,shopName} = req.query;
        let file = req.files.multipartFile;
        let userId = req.query.userId;
        const uri = file.path
        let multipartFile = new FormData();
        multipartFile.append('file', fs.createReadStream(uri));
        console.log(introduction)
        console.log(userId)
        console.log(shopAddress)
        console.log(shopName)
        axios.post(
            // `/bussiness/addShop?introduction=${encodeURI(
                //     introduction
                // )}userId${encodeURI(
                //     userId
                // )}shopAddress${encodeURI(
                //     shopAddress  
                // )}shopName${encodeURI(
                //     shopName
                // )}
                `http://www.mianded.top:8081/bussiness/addShop?shopAddress=${encodeURI(
                    shopAddress  
                )}&shopName=${encodeURI(
                    shopName
                )}&userId=${encodeURI(
                    userId
                )}&introduction=${encodeURI(
                    introduction
                )}`
                ,multipartFile, { headers: multipartFile.getHeaders() }).then((data) => {
            console.log("success============")
            console.log(data.data)
            res.send({ data: data.data })
        }).catch((error) => {
            console.log("err");
            res.send(error)
        })
})
//注销店铺
shopstage.delete('/bussiness/deleteShop', (req, res) => {
    console.log(req.query)
    let {shopId} = req.query;
    axios({
        url: '/bussiness/deleteShop',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            token:req.session.token
        },
        method: 'delete',
        params: {
            shopId:shopId,
            userId:jwt.decode(req.session.token).id
        }
    }).then(function(data){
        res.status(200).json({
            data:data.data,
            code:0
        })
    }).catch(function(err){
        res.status(500).json({
            msg:err
        })
    })
})
//获取店铺信息
shopstage.get('/bussiness/getShopInfo', (req, res) => {
    // console.log(req.query)
    let {shopId} = req.query;
    axios({
        url: '/bussiness/getShopInfo',
        method: 'get',
        params: {
            shopId:shopId
        }
    }).then(function(data){
        res.status(200).json({
            data:data.data,
            code:0
        })
    }).catch(function(err){
        res.status(500).json({
            msg:err
        })
    })
})
//修改店铺信息
shopstage.post('/bussiness/updateShopInfo', (req, res) => {
    // console.log(req.body)
    // console.log(10086)
    let {shopId,shopAddress,userId,shopName,introduction} = req.body.params;
    axios({
        url: '/bussiness/updateShopInfo',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            token:req.session.token
        },
        method: 'post',
        params: {
            shopName:shopName,
            shopAddress:shopAddress,
            userId:jwt.decode(req.session.token).id,
            introduction:introduction,
            shopId:shopId
        }
    }).then(function(data){
        res.status(200).json({
            data:data.data,
            code:0
        })
    }).catch(function(err){
        res.status(500).json({
            msg:err
        })
    })
})
//删除某一个商品
shopstage.delete('/bussiness/deleteOneGood', (req, res) => {
    // console.log(req.query)
    let {userId,goodsId}=req.query
   axios({
        method:'delete',
        url : '/bussiness/deleteOneGood',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            token:req.session.token
        },
        params:{
            userId:jwt.decode(req.session.token).id,
            goodsId:goodsId
        }
   }).then(function(data){
       res.status(200).json({
           data:data.data,
           code:0
       })
   }).catch(function(err){
       res.status(500).json({
           msg:err
       })
   })
})
// 查询所有商品
shopstage.get('/bussiness/getBusinessAllGoods', (req, res) => {
    // console.log(req.body)
    let {userId} = req.query;
    axios({
        url: '/bussiness/getBusinessAllGoods',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            token:req.session.token
        },
        method: 'get',
        params: {
            userId:jwt.decode(req.session.token).id
        }
    }).then(function(data){
        res.status(200).json({
            data:data.data,
            code:0
        })
    }).catch(function(err){
        res.status(500).json({
            msg:err
        })
    })
})
shopstage.get('/bussiness/getShopAllGoods', (req, res) => {
    // console.log(req.query)
    let {shopId,page,pageSize} = req.query;
    axios({
        url: '/bussiness/getShopAllGoods',
        method: 'get',
        params: {
            shopId:shopId,
            page:page,
            pageSize:pageSize
        }
    }).then(function(data){
        res.status(200).json({
            data:data.data,
            code:0
        })
    }).catch(function(err){
        res.status(500).json({
            msg:err
        })
    })
})
//查询所有评论
shopstage.get('/comment/getAllCommentsByPage', (req, res) => {
    // console.log(1)
    // console.log(req.query)
    let {shopId,page,pageSize} = req.query;
    axios({
        url: '/comment/getAllCommentsByPage',
        method: 'get',
        params: {
            shopId:shopId,
            page:page,
            pageSize:pageSize
        }
    }).then(function(data){
        console.log(1)
        console.log(data.data)
        res.status(200).json({
            data:data.data,
            code:0
        })
    }).catch(function(err){
        res.status(500).json({
            msg:err
        })
    })
})
//获取所有订单
shopstage.get('/bussiness/getAllOrderByShopId', (req, res) => {
    // console.log(req.query)
    let {shopId,page,pageSize} = req.query;
    axios({
        url: '/bussiness/getAllOrderByShopId',
        method: 'get',
        params: {
            shopId:shopId,
            page:page,
            pageSize:pageSize
        }
    }).then(function(data){
        res.status(200).json({
            data:data.data,
            code:0
        })
    }).catch(function(err){
        res.status(500).json({
            msg:err
        })
    })
})
//用户退款
shopstage.get('/bussiness/getAllRefundByShopId', (req, res) => {
    // console.log(req.query)
    let {shopId,page,pageSize} = req.query;
    axios({
        url: '/bussiness/getAllRefundByShopId',
        method: 'get',
        params: {
            shopId:shopId,
            page:page,
            pageSize:pageSize
        }
    }).then(function(data){
        res.status(200).json({
            data:data.data,
            code:0
        })
    }).catch(function(err){
        res.status(500).json({
            msg:err
        })
    })
})
//新增快递信息
shopstage.post('/express/addExpress', (req, res) => {
    // console.log(req.body.params)
    let {expressBrandId,expressTypeId,orderId,remarks,starting} = req.body.params;
    axios({
        url: '/express/addExpress',
        method: 'post',
        params: {
            expressBrandId:expressBrandId,
            expressTypeId:expressTypeId,
            orderId:orderId,
            remarks:remarks,
            starting:starting
        }
    }).then(function(data){
        res.status(200).json({
            data:data.data,
            code:0
        })
    }).catch(function(err){
        res.status(500).json({
            msg:err
        })
    })
})
//确认发货
shopstage.put('/express/businessSureSent', (req, res) => {
    // console.log(req.body.params)
    let {expressId}=req.body.params
   axios({
       method:'put',
       url : '/express/businessSureSent',
       params:{
        expressId:expressId
    }
   }).then(function(data){
       res.status(200).json({
           data:data.data,
           code:0
       })
   }).catch(function(err){
       res.status(500).json({
           msg:err
       })
   })
})

module.exports=shopstage;
